<?php
/**
 * Controller Applications
 */
include_once 'dao/Question.php';
class ControllerQuestion
{   
	public $sortspis;
	public $typepage;
	public $mvcData;
	private $smarty;
	
	/**
	 * 
	 * Data acquisition mapping
	 */
	public function __construct()
	{
		$spis = new DaoQuestion();
		$this->sortspis = $spis->GetData();
	}
	
	/**
	 * Validate form to the correctness of filling
	 * 
	 * @param $listquestion array questions and answer
	 * @param $post array answer user
	 * 
	 * @return $questionnoreply array number Question unanswered
	 */
	private function validateForm($listquestion, $post) 
	{
		$questionnoreply = array();
		for ($i = 0; $i < count($listquestion); $i++)
		{
			$validform = array();
			for ($j = 0; $j < count($listquestion[$i]->getAnswers()); $j++) 
			{
				if ($post[question][$i][$j]!= NULL)
				{
					$validform[]=$j;		
				}
			}
			if (count($validform)==0)
			{
				$questionnoreply[]=$i;
			}
		}
		return $questionnoreply;
	}
	
	/**
	 * Select a page display
	 * 
	 * @param $post array answer user
	 * 
	 */
	public function determinantPage($post, $get)
	{
		if (empty($post))
		{   
			$this->typepage = 'form';
			$this->mvcData = array('typepage' => $this->typepage,
									 'questionnoreply' => null,
									 'post' => $post,
									 'listquestion' => $this->sortspis);
		}
		else
		{   
			$formvalid = $this->validateForm($this->sortspis, $post);
			if (count($formvalid)==0)
			{
				$this->typepage = 'result';
				$this->mvcData = array('typepage' => $this->typepage,
									   'questionnoreply' => null,
									   'post' => $post,
									   'listquestion' => $this->sortspis);
			}	
			else
			{
				$this->typepage = 'form';
				$this->mvcData = array('typepage' => $this->typepage,
									   'questionnoreply' => $formvalid,
									   'post' => $post,
									   'listquestion' => $this->sortspis);
			}
		}
	}
	
	public function getMvcData() 
    { 
    	return $this->mvcData;
    } 
	
}